<?php
namespace Admin\Controller;
use Think\Controller;
class SyssetController extends CommonController {
	//系统设置显示
    public function index()
    {
    	//邮箱信息
    	$sql = 'select * from sys_set where id=3';
    	$emailinfo = M()->query($sql);
    	$emailinfo = current($emailinfo);
    	$this->assign('emailinfo',$emailinfo);

      //网站信息
      $siteinfo = M('sys_siteinfo')->where('id=1')->find();
      $this->assign('siteinfo',$siteinfo);

      //允许的访问ip
      $allow_ip_list = M('allow_ip_list')->select();
      $ips = array();
      foreach ($allow_ip_list as $v) {
          $ips[] = $v['allow_ip'];
      }
      $allow_ip_list = implode(',', $ips);
      $this->assign('allow_ip_list',$allow_ip_list);

        $this->display();
    }

    //网站详细信息
    public function site_info()
    {
      //print_r($_POST);
      //Array ( [site_name] => aa [keyword] => a [miaoshu] => a [css_js_path] => aa [site_path] => a [copyright] => a [beianhao] => a [tongjidaima] => a )
      if (IS_POST) {
        $data['site_name'] = I('post.site_name');
        $data['keyword'] = I('post.keyword');
        $data['miaoshu'] = I('post.miaoshu');
        $data['css_js_path'] = I('post.css_js_path');
        $data['site_path'] = I('post.site_path');
        $data['copyright'] = I('post.copyright');
        $data['beianhao'] = I('post.beianhao');
        $data['tongjidaima'] = I('post.tongjidaima');

        if (empty($data['site_name'])||empty($data['site_path'])||empty($data['beianhao'])) {
          $arr = array(
              'error' => 0,
              'info' => '网站名,网站路径,备案号不能为空'
            );
          echo json_encode($arr);
          exit;
        }
        $res = M('sys_siteinfo')->where('id=1')->save($data);
        if ($res) {
          $arr = array(
              'error' => 1,
              'info' => '保存成功'
            );
          echo json_encode($arr);
        }else{
          $arr = array(
              'error' => 0,
              'info' => '保存失败'
            );
          echo json_encode($arr);
        }
      }
    }

    //允许访问的ip列表
    public function allow_ip()
    {
      if (IS_POST) {
        $data['allow_ip'] = I('post.allow_ip');
        $data['allow_max'] = I('post.allow_num');

        if (empty($data['allow_ip'])||empty($data['allow_max'])) {
          $arr = array(
              'error' => 0,
              'info' => 'ip,次数不能为空'
            );
          echo json_encode($arr);
          exit;
        }
        $res = M('allow_ip_list')->add($data);
        if ($res) {
          $arr = array(
              'error' => 1,
              'info' => '添加成功'
            );
          echo json_encode($arr);
        }else{
          $arr = array(
              'error' => 0,
              'info' => '添加失败'
            );
          echo json_encode($arr);
        }

      }

    }

    //提交服务器激活邮箱 信息
    public function server_email()
    {
    	//print_r($_POST);
		if (IS_POST) {
			$data['server_smtp'] = I('post.server_smtp');
			$data['email_port'] = I('post.email_port');
			$data['smtp_username'] = I('post.smtp_username');
			$data['smtp_password'] = I('post.smtp_password');
			$data['site_email'] = I('post.site_email');

			$data['set_time'] = time();

			$res = M('sys_set')->where('id=3')->save($data);
			if ($res) {
				$arr = array(
					'error' =>1,
					'info' =>'邮箱添加成功'
					);
				echo json_encode($arr);
			}else {
				$arr = array(
					'error' =>0,
					'info' =>'邮箱添加失败'
					);
				echo json_encode($arr);
			}
		}
    }

    //服务器 前台网站关闭/开启
    public function close_on_site()
    {
    	//print_r($_POST);
    	$set_state = I('post.open');
    	if ($set_state == 'on') {
    		$data['set_state'] = 1;
			$data['set_time'] = time();

			$res = M('sys_set')->where('id=1')->save($data);
			if ($res) {
				$arr = array(
					'error' =>1,
					'info' =>'网站开启成功'
					);
				echo json_encode($arr);
			}else {
				$arr = array(
					'error' =>0,
					'info' =>'网站开启失败'
					);
				echo json_encode($arr);
			}
    	}else {
    		$data['set_state'] = 0;
			$data['set_time'] = time();

			$res = M('sys_set')->where('id=1')->save($data);
			if ($res) {
				$arr = array(
					'error' =>1,
					'info' =>'网站关闭成功'
					);
				echo json_encode($arr);
			}else {
				$arr = array(
					'error' =>0,
					'info' =>'网站关闭失败'
					);
				echo json_encode($arr);
			}
    	}
    }

    //系统日志 显示
    public function sys_log()
    {
    	$sql = 'select * from sys_log';
    	$sys_log = M()->query($sql);
    	$this->assign('sys_log',$sys_log);

    	$this->display();
    }

    //批量删除
    public function log_deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        $res = M('sys_log')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"系统日志删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"系统日志删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //log_del 删除
    public function log_del()
    {
      if (IS_POST) {
          $sys_logid = I('post.sys_logid');
          $sql = 'delete from sys_log where id='.$sys_logid;
          $log_del = M()->execute($sql);
          if ($log_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"系统日志删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"系统日志删除失败"
                  );
              echo json_encode($arr);
          }
        }       
    }



}